<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<title>卖家发布商品</title>
	<link rel="stylesheet" type="text/css" href="css/publish.css"/>
	<script src="js/jquery-2.1.0.js"></script>


	<script type="text/javascript">

		//下面用于多图片上传预览功能

		function setImagePreviews(avalue) {

			var docObj = document.getElementById("doc");

			var dd = document.getElementById("dd");

			dd.innerHTML = "";

			var fileList = docObj.files;

			for (var i = 0; i < fileList.length; i++) {
				dd.innerHTML += "<div style='float:left' > <img id='img" + i + "'  /> </div>";

				var imgObjPreview = document.getElementById("img"+i);

				if (docObj.files && docObj.files[i]) {

					//火狐下，直接设img属性

					imgObjPreview.style.display = 'block';

					imgObjPreview.style.width = '80px';

					imgObjPreview.style.height = '80px';

					imgObjPreview.style.marginLeft='30px';

					//imgObjPreview.src = docObj.files[0].getAsDataURL();

					//火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式

					imgObjPreview.src = window.URL.createObjectURL(docObj.files[i]);

				}

				else {

					//IE下，使用滤镜

					docObj.select();

					var imgSrc = document.selection.createRange().text;
					alert(imgSrc)

					var localImagId = document.getElementById("img" + i);

					//必须设置初始大小

					localImagId.style.width = "80px";

					localImagId.style.height = "80px";
					localImagId.style.marginLeft='30px';

					//图片异常的捕捉，防止用户修改后缀来伪造图片

					try {

						localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";

						localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;

					}
					catch (e) {

						alert("您上传的图片格式不正确，请重新选择!");

						return false;

					}
					imgObjPreview.style.display = 'none';

					document.selection.empty();

				}

			}
			return true;

		}
		
		function getUrl(){
		var str = location.href;//获取传过来的地址
		var arr = str.split("?");
		var str1 = arr[1];//获取？之后的内容
		sellerId = str1.split("=")[1];
		return sellerId;
	}
	</script>



</head>
<body>
<div id="back">
	<a href="#" onClick="javascript :history.back(-1);"><&nbsp;返回</a><!--将返回的地址填入-->
</div>
<div id="goodsPublish">
	<input type="text" name="goodsName" id="goodsName" value="" placeholder="商品名称"/><br />
	<textarea name="goodsDesp" id="goodsDesp" rows="8" value="" placeholder="请输入商品描述" maxlength="50"></textarea><br />
	<!--<input type="file" name="imgInput" id="imgInput" value="" placeholder="请选择图片"/><br />-->


	<div style="width:100%;height: 100px;">
		<form action="" method="post" enctype="multipart/form-data" id="form1" target="a">
			<a id="imgoutdiv">+选择图片
				<input type="file"  name="file" id="doc" multiple="multiple"  style="width:150px;" onchange="javascript:setImagePreviews();" accept="image/*" />
			</a>
		</form>
		<iframe name="a" style="display: none;">			
		</iframe>
		<div id="dd" style=" width:100%;"></div>

	</div>
	<select name="family" id="family">
		<option value="tu" selected>土特产</option>
		<option value="water">水产</option>
		<option value="fruit">水果</option>
		<option value="local">小吃</option>
	</select>


	<input type="text" name="originPlace" id="originPlace" value="" placeholder="请输入产地"/><br />
	<input type="text" name="schoolrange" id="schoolrange" value="" placeholder="请输入您所要上架的目的学校"/>
	<input type="number" name="goodsPrice" id="goodsPrice" value="" placeholder="请输入单价并选择单位"/>
	<select name="danwei" id="danwei">
		<option value="1" selected="selected">/g</option>
		<option value="2">/kg</option>
		<option value="3">/袋</option>
	</select>
	<br />
	<br />
	<input type="number" name="goodsnum" id="goodsnum" value="" placeholder="请输入商品数量"/>
	<input type="button" name="goodsSub" id="goodsSub" value="提交" disabled="disabled"/>
</div>
<script>


	//				首先通过userId搜索卖家id
	function getUrl(){
		var str = location.href;//获取传过来的地址
		var arr = str.split("?");
		var str1 = arr[1];//获取？之后的内容
		sellerId = str1.split("=")[1];
		return sellerId;
	}

//						在执行后面的函数
			GoodButton(getUrl());


	function GoodButton(sellerId){
		var sub = $("#goodsSub");
		sub.click(function(){
			var Name = $("#goodsName").val();
			var goodsDescrip = $("#goodsDesp").text();
			var Place = $("#originPlace").val();
			var school = $("#schoolrange").val();
			var price = $("#goodsPrice").val();
			var sum = $("#goodsnum").val();
			var danWei = $("#danwei option:selected").text();
			var Category = $("#family option:selected").text();
			$.ajax("/fengwei/goods/insert",{
				type:"post",
				data:{
					'sellerId':sellerId,
					'goodsName':Name,
					'goodsPrice':price,
					'goodsIntroducts':goodsDescrip,
					'goodsCity':Place,
					'goodsSchool':school,
					'goodsSize':sum,
					'goodsUnit':danWei,
					'goodCategory':Category
				},
				success:function(data){
//								返回的数据里面有商品id,根据商品id去上传图片
					alert(1);
					var dataObj1 = eval('('+data+')');
					console.log(dataObj1);
					var goodsId = dataObj1.goodsId;
					addId(goodsId);
					setTimeout(function(){
								window.location.href = "goodsManage.html?sellerId="+getUrl()+"";
					},5000);
					
			
				},
				error:function(){
					alert(0)
				},
		
			});


			function addId(goodsId){
				var form1 = document.getElementById("form1");
				form1.action = '/fengwei/file/test?goodsId='+goodsId+'';
				form1.submit();
//				window.location.href = "goodsManage.html?sellerId="+getUrl()+"";
			}

		})
	}

</script>

<script type="text/javascript">
	var goodsName=document.getElementById('goodsName');
	var goodsDesp=document.getElementById('goodsDesp');
	var originPlace=document.getElementById('originPlace');
	var schoolrange=document.getElementById('schoolrange');
	var goodsPrice=document.getElementById('goodsPrice');
	var goodsSubButton=document.getElementById('goodsSub');

	goodsName.oninput=goodsDesp.oninput=originPlace.oninput=schoolrange.oninput=goodsPrice.oninput=function(){
		if(goodsName.value!=''&&goodsDesp.value!=''&&originPlace.value!=''&&goodsPrice.value!=''&&schoolrange.value!=''){
			goodsSubButton.style.background='#FFCC33';
			goodsSubButton.disabled=false;
		}
		else{
			goodsSubButton.style.background='#d1d1d1';
			goodsSubButton.disabled=true;
		}
	}
</script>
</body>
</html>
